package com.xiaohai.exceptionhandle.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * 将返回信息封装成json字符串
 */
public class JsonResponse {


    public static  String ERR(String stateCode, String message){
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String,String> map =new HashMap<String,String>();
        map.put("code",stateCode);
        map.put("msg",message);
        try {
          return   objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "json化异常";
    }

    public static  String OK(Object data){
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("code",StatusCode.Sucess);
        map.put("data",data);
        try {
            return   objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "json化异常";
    }
}
